package com.jeecg.sms.job;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.util.DynamicDBUtil;
import org.jeecgframework.core.util.HttpRequest;
import org.jeecgframework.core.util.MutiLangUtil;
import org.jeecgframework.core.util.ResourceUtil;
import org.jeecgframework.web.system.pojo.base.DictEntity;
import org.jeecgframework.web.system.pojo.base.TSType;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jeecg.sms.entity.RptSmsSendEntity;
import com.jeecg.sms.service.RptSmsSendServiceI;

@Service("chunYunMessageTask")
public class ChunYunMessageJob{

	private final static Logger  logger = Logger.getLogger(ChunYunMessageJob.class);
	@Autowired
	private RptSmsSendServiceI rptSmsSendService;
	@Autowired
	private SystemService systemService;
		
	/**
	 * 生成生产日报短信
	 */
	public void run() {
		//org.jeecgframework.core.util.LogUtil.info("===================消息中间件定时任务开始===================");
		List<TSType> configList = ResourceUtil.allTypes.get("conf");
		String chunshuyun="春运";
		String tb="20180201";
		String te="20180312";
		String lb="20170113";
		String le="20170221";
		String pjc="54916";
		String plk="8190114";
		String phy="143979430";
		if (configList != null && !configList.isEmpty()) {
			for (TSType type : configList) {
				if (type.getTypename().equals("chunshuyun")) 
					chunshuyun=type.getTypecode();
				if (type.getTypename().equals("tb")) 
					tb = type.getTypecode();
				if (type.getTypename().equals("te")) 
					te=type.getTypecode();
				if (type.getTypename().equals("lb")) 
					lb=type.getTypecode();
				if (type.getTypename().equals("le")) 
					le=type.getTypecode();
				if (type.getTypename().equals("pjc")) 
					pjc=type.getTypecode();
				if (type.getTypename().equals("plk")) 
					plk=type.getTypecode();
				if (type.getTypename().equals("phy")) 
					phy=type.getTypecode();
			}
		}
		String lyear=le.substring(0,4);

		Date today=new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		try {
			today=sdf.parse(te);			
			Calendar c = Calendar.getInstance();  
	        c.setTime(today);
	        c.add(Calendar.DAY_OF_MONTH, 2);
	        //
			if(sdf.parse(tb).compareTo(new Date())<0 && c.getTime().compareTo(new Date())>0)
			{}
			else
				return;
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		logger.info("===================春运短信生成定时任务开始===================");
		String sql="select to_char(to_date(t.rq,'yyyymmdd'),'yyyy\"年\"mm\"月\"dd\"日\"') as c1,"+
				"ceil(sysdate-1-to_date("+tb+",'yyyymmdd')) as d,"+
				"(select count(1) from flt_cd_i_aomip a where a.sjsj like to_char(to_date(t.rq,'yyyymmdd'),'yyyy-mm-dd')||'%') as c2,"+
				"(select round((sum(d.ADNO)+sum(d.CHNO)+sum(d.PASS_ADNO)+sum(d.PASS_CDNO))/10000,4) from flt_all_data d where qjrq=t.rq and d.state = 0) as c5,"+
				"(select round((sum(d.frew)+sum(d.POSW))/1000,2) from flt_all_data d where qjrq=t.rq and d.state = 0) as c8,"+
				"(select round((sum(d.ADNO)+sum(d.CHNO)+sum(d.PASS_ADNO)+sum(d.PASS_CDNO))/10000,4) from flt_all_data d where qjrq=t.rq and d.state = 0 and (d.flrt_type = 'INT' or d.flrt_type = 'REG')) as c9,"+
				"(select round((th.n-la.n)*100/la.n,2) from (select count(1) n from flt_cd_i_aomip a where a.sjsj like to_char(to_date(t.rq,'yyyymmdd'),'yyyy-mm-dd')||'%') th,(select (a.c+b.c) as n from (select sum(cont) as c from flt_all_data where state =0 and qjrq=lemd) a, (select COALESCE(sum(qfjc),0)+COALESCE(sum(jljc),0) as c from tb_cd_whx  where rq=t.lemd) b) la) as c12,"+
				"(select round((th.n-la.n)*100/la.n,2) from (select (sum(d.ADNO)+sum(d.CHNO)+sum(d.PASS_ADNO)+sum(d.PASS_CDNO)) as n from flt_all_data d where qjrq=t.rq and d.state = 0) th,(select (sum(d.ADNO)+sum(d.CHNO)+sum(d.PASS_ADNO)+sum(d.PASS_CDNO)) as n from flt_all_data d where qjrq=t.lemd and d.state = 0) la) as c13,"+
				"(select round((th.n-la.n)*100/la.n,2) from (select (sum(d.frew)+sum(d.POSW)) as n from flt_all_data d where qjrq=t.rq and d.state = 0) th,(select (sum(d.frew)+sum(d.POSW)) as n from flt_all_data d where qjrq=t.lemd and d.state = 0) la) as c14,"+
				"(select round((th.n-la.n)*100/la.n,2) from (select (sum(d.ADNO)+sum(d.CHNO)+sum(d.PASS_ADNO)+sum(d.PASS_CDNO)) as n from flt_all_data d where qjrq=t.rq and d.state = 0 and (d.flrt_type = 'INT' or d.flrt_type = 'REG')) th,(select (sum(d.ADNO)+sum(d.CHNO)+sum(d.PASS_ADNO)+sum(d.PASS_CDNO)) as n from flt_all_data d where qjrq=t.lemd and d.state = 0 and (d.flrt_type = 'INT' or d.flrt_type = 'REG')) la) as c15,"+
				"to_char(to_date(t.bmd,'yyyymmdd'),'yyyy\"年\"mm\"月\"dd\"日\"') as c16,to_char(to_date(t.rq,'yyyymmdd'),'yyyy\"年\"mm\"月\"dd\"日\"') as c17,"+
				"(select  round((a.c+b.c)/10000,4) from (select sum(cont) as c from flt_all_data where state =0 and qjrq between bmd and t.rq) a, (select COALESCE(sum(qfjc),0)+COALESCE(sum(jljc),0) as c from tb_cd_whx  where rq between bmd and t.rq) b) as c18,"+
				"(select round((sum(d.ADNO)+sum(d.CHNO)+sum(d.PASS_ADNO)+sum(d.PASS_CDNO))/10000,4) from flt_all_data d where qjrq between bmd and t.rq and d.state = 0) as c19,"+
				"(select round((sum(d.frew)+sum(d.POSW))/10000000,4) from flt_all_data d where qjrq between bmd and t.rq and d.state = 0) as c20,"+
				"(select round((sum(d.ADNO)+sum(d.CHNO)+sum(d.PASS_ADNO)+sum(d.PASS_CDNO))/10000,4) from flt_all_data d where qjrq between bmd and t.rq and d.state = 0 and (d.flrt_type = 'INT' or d.flrt_type = 'REG')) as c21,"+
				"(select  round((a.c+b.c)/(to_date(t.rq,'yyyymmdd')-to_date(t.bmd,'yyyymmdd')+1),0) from (select sum(cont) as c from flt_all_data where state =0 and qjrq between bmd and t.rq) a, (select COALESCE(sum(qfjc),0)+COALESCE(sum(jljc),0) as c from tb_cd_whx  where rq between bmd and t.rq) b) as c22,"+
				"(select round((sum(d.ADNO)+sum(d.CHNO)+sum(d.PASS_ADNO)+sum(d.PASS_CDNO))/((to_date(t.rq,'yyyymmdd')-to_date(t.bmd,'yyyymmdd')+1)*10000),4) from flt_all_data d where qjrq between bmd and t.rq and d.state = 0) as c23,"+
				"(select round((sum(d.frew)+sum(d.POSW))/((to_date(t.rq,'yyyymmdd')-to_date(t.bmd,'yyyymmdd')+1)*1000),2) from flt_all_data d where qjrq between bmd and t.rq and d.state = 0) as c24,"+
				"(select round((sum(d.ADNO)+sum(d.CHNO)+sum(d.PASS_ADNO)+sum(d.PASS_CDNO))/((to_date(t.rq,'yyyymmdd')-to_date(t.bmd,'yyyymmdd')+1)*10000),4) from flt_all_data d where qjrq between bmd and t.rq and d.state = 0 and (d.flrt_type = 'INT' or d.flrt_type = 'REG')) as c25,"+
				"(select round((th.n-la.n)*100/la.n,2) from (select (a.c+b.c) as n  from (select sum(cont) as c from flt_all_data where state =0 and qjrq between bmd and t.rq) a, (select COALESCE(sum(qfjc),0)+COALESCE(sum(jljc),0) as c from tb_cd_whx  where rq between bmd and t.rq) b) th,(select (a.c+b.c) as n from (select sum(cont) as c from flt_all_data where state =0 and qjrq between t.lbmd and t.lemd) a, (select COALESCE(sum(qfjc),0)+COALESCE(sum(jljc),0) as c from tb_cd_whx  where rq between t.lbmd and t.lemd) b) la) as c26,"+
				"(select round((th.n-la.n)*100/la.n,2) from (select (sum(d.ADNO)+sum(d.CHNO)+sum(d.PASS_ADNO)+sum(d.PASS_CDNO)) as n from flt_all_data d where qjrq between bmd and t.rq and d.state = 0) th,(select (sum(d.ADNO)+sum(d.CHNO)+sum(d.PASS_ADNO)+sum(d.PASS_CDNO)) as n from flt_all_data d where qjrq between t.lbmd and t.lemd and d.state = 0) la) as c27,"+
				"(select round((th.n-la.n)*100/la.n,2) from (select (sum(d.frew)+sum(d.POSW)) as n from flt_all_data d where qjrq between bmd and t.rq and d.state = 0) th,(select (sum(d.frew)+sum(d.POSW)) as n from flt_all_data d where qjrq between t.lbmd and t.lemd and d.state = 0) la) as c28,"+
				"(select round((th.n-la.n)*100/la.n,2) from (select (sum(d.ADNO)+sum(d.CHNO)+sum(d.PASS_ADNO)+sum(d.PASS_CDNO)) as n from flt_all_data d where qjrq between bmd and t.rq and d.state = 0 and (d.flrt_type = 'INT' or d.flrt_type = 'REG')) th,(select (sum(d.ADNO)+sum(d.CHNO)+sum(d.PASS_ADNO)+sum(d.PASS_CDNO)) as n from flt_all_data d where qjrq between t.lbmd and t.lemd and d.state = 0 and (d.flrt_type = 'INT' or d.flrt_type = 'REG')) la) as c29,"+
				"(select round((a.c+b.c)*100/"+pjc+",2) from (select sum(cont) as c from flt_all_data where state =0 and qjrq between bmd and t.rq) a, (select COALESCE(sum(qfjc),0)+COALESCE(sum(jljc),0) as c from tb_cd_whx  where rq between bmd and t.rq) b) as c30,"+
				"(select round((sum(d.ADNO)+sum(d.CHNO)+sum(d.PASS_ADNO)+sum(d.PASS_CDNO))*100/"+plk+",2) as n from flt_all_data d where qjrq between bmd and t.rq and d.state = 0) as c31,"+
				"(select round((sum(d.frew)+sum(d.POSW))*100/"+phy+",2) as n from flt_all_data d where qjrq between bmd and t.rq and d.state = 0) as c32,"+
				"(select round(th.n*100/ah.n,2) from (select (sum(d.ADNO)+sum(d.CHNO)+sum(d.PASS_ADNO)+sum(d.PASS_CDNO)) as n from flt_all_data d where qjrq between bmd and t.rq and d.state = 0 and (d.flrt_type = 'INT' or d.flrt_type = 'REG')) th,(select (sum(d.ADNO)+sum(d.CHNO)+sum(d.PASS_ADNO)+sum(d.PASS_CDNO)) as n from flt_all_data d where qjrq between bmd and t.rq and d.state = 0) ah) as c33"+
				"  from (select to_char(sysdate-1,'yyyymmdd') as rq,"+tb+" as bmd,"+lb+" as lbmd,to_char((to_date("+lb+",'yyyymmdd')+abs(sysdate-1-trunc(to_date("+tb+",'yyyymmdd')))),'yyyymmdd') as lemd from dual) t";
		List<Map<String,Object>> dataList =  DynamicDBUtil.findList("production_db", sql);
		if(dataList.size()>0)
		{
			String content= chunshuyun+"快报\r\n"+dataList.get(0).get("C1")+"（"+chunshuyun+"第"+dataList.get(0).get("D")+"天）航班、旅客、货邮和国际旅客分别是"+dataList.get(0).get("C2")+"架次、"+dataList.get(0).get("C5")+"万人次、"+dataList.get(0).get("C8")+"吨和"+dataList.get(0).get("C9")+"万人次，"+
					"与"+lyear+"年"+chunshuyun+"同比分别增长"+dataList.get(0).get("C12")+"%、"+dataList.get(0).get("C13")+"%、"+dataList.get(0).get("C14")+"%和"+dataList.get(0).get("C15")+"%。\r\n"+
					dataList.get(0).get("C16")+"至"+dataList.get(0).get("C17")+"航班起降"+dataList.get(0).get("C18")+"万架次，日平均"+dataList.get(0).get("C22")+"架次，与"+lyear+"年"+chunshuyun+"同比增长"+dataList.get(0).get("C26")+"%，完成"+chunshuyun+"计划的"+dataList.get(0).get("C30")+"%；"+
					"旅客"+dataList.get(0).get("C19")+"万人次，日平均"+dataList.get(0).get("C23")+"万人次，与"+lyear+"年"+chunshuyun+"同比增长"+dataList.get(0).get("C27")+"%，完成"+chunshuyun+"计划的"+dataList.get(0).get("C31")+"%；"+
					"货邮"+dataList.get(0).get("C20")+"万吨，日平均"+dataList.get(0).get("C24")+"吨，与"+lyear+"年"+chunshuyun+"同比增长"+dataList.get(0).get("C28")+"%，完成"+chunshuyun+"计划的"+dataList.get(0).get("C32")+"%；"+
					"国际旅客"+dataList.get(0).get("C21")+"万人次，日平均"+dataList.get(0).get("C25")+"万人次，与"+lyear+"年"+chunshuyun+"同比增长"+dataList.get(0).get("C29")+"%，占总旅客的比重为"+dataList.get(0).get("C33")+"%。（白云机场运行控制中心）";
			//发送信息到接口
			//春运简报接收人
			List<TSType> typeList = ResourceUtil.allTypes.get("0003");
			String phones="";
			int index = 0;
			if (typeList != null && !typeList.isEmpty()) {
				for (TSType type : typeList) {
					if (index>0) phones +=",";
					phones +=  type.getTypecode();
					index++;
				}
			}
			List<TSType> typeList2 = ResourceUtil.allTypes.get("0001");
			String url="";
			String password="";
			if (typeList != null && !typeList.isEmpty()) {
				for (TSType type : typeList2) {
					if (type.getTypename().equals("url")) 
						url = type.getTypecode();
					if (type.getTypename().equals("password")) 
						password=type.getTypecode();
				}
			}
			try {
				content =java.net.URLEncoder.encode(content,"utf-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			String param ="password="+password+"&text="+content+"&recipient="+phones+"&encoding=U";

			String result =HttpRequest.sendGet(url, param);
			logger.info(result);
		}
		else
		{
			logger.info("没有查询到当日生产数据===================");
		}
		logger.info("===================春运短信生成定时任务结束===================");
	}
}
